import store from '../../store'
import { ElMessage } from '_element-plus@1.0.2-beta.48@element-plus'

export default {
  data () {
    var id
    var username = ''
    var password = ''
    return {
      id,
      username,
      password
    }
  },
  methods: {
    login () {
      if (this.username === '' || this.password === '') {
        ElMessage.error('用户名或密码不能为空')
        return
      }
      var userInfo = {
        username: this.username,
        password: this.password
      }
      store.dispatch('login', userInfo)
        .then(response => {
          const data = response.data
          if (data.message === 'success') {
            const token = data.token
            const user = data
            store.commit('SET_TOKEN', token)
            store.commit('SET_USER', user)
            this.$router.push({ name: 'Asset' })
          } else {
            ElMessage.error('用户名或密码错误')
          }
        })
        .catch(error => {
          console.log('connect failed')
          console.log(error)
        })
    },
    register () {
      this.$router.push({ name: 'register' })
    }
  }
}
